递归：
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def postorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
        res = []
        def printf(root):
            nonlocal res
            if root:
                printf(root.left)
                printf(root.right)
                res.append(root.val)
        printf(root)
        return res

迭代：

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def postorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
        T = []
        res = []
        pre = None
        while root or len(T):
            while root :
                T.append(root)
                root = root.left
            if len(T):
                top = T[-1]
                if top.right == None or top.right == pre:
                    pre = top
                    res.append(top.val)
                    T = T[:-1]
                else:
                    root = top.right
        return res